﻿#region Copyright 
/*-------------------------------------------------------------------------
* 命名空间名称/文件名:    SSPivot.AspNetCore.DependencyInjection/RegisterAssembly 
* CLR版本:     4.0.30319.42000
* 机器名称:    DESKTOP-NHMLO8A
* 功 能：       N/A
* 类 名：       RegisterAssembly
* 创建时间：  2025/6/3 16:53:05
* 版本号：      v1.0
* 创建人:        xulong
*-------------------------------------------------------------------------*/
#endregion
using Autofac;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SSPivot.Common.Interface;
using SSPivot.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace SSPivot.AspNetCore.DependencyInjection
{
    public static class AutoFacExtensions
    {
        /// <summary>
		///  注册程序集Ioc
		/// </summary>
		public static void RegisterAssembly(this ContainerBuilder builder, Assembly[] assemblys)
        {
              builder.RegisterAssemblyTypes(assemblys).Where(t => typeof(IScopedDependency).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface).As(t => t.GetInterfaces().Where(t => !t.Equals(typeof(IScopedDependency)))).AsSelf().InstancePerRequest().PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies);
            builder.RegisterAssemblyTypes(assemblys).Where(t => typeof(ITransientDependency).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface).As(t => t.GetInterfaces().Where(t => !t.Equals(typeof(ITransientDependency)))).AsSelf().InstancePerLifetimeScope().PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies);
            builder.RegisterAssemblyTypes(assemblys).Where(t => typeof(ISingletonDependency).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface).As(t => t.GetInterfaces().Where(t => !t.Equals(typeof(ISingletonDependency)))).AsSelf().SingleInstance().PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies);
            builder.RegisterType<HttpContextAccessor>().As<IHttpContextAccessor>().SingleInstance().PropertiesAutowired();
            builder.RegisterType<AppContextProvider>().SingleInstance().PropertiesAutowired();
            builder.RegisterAssemblyTypes(assemblys).Where(t => t.IsSubclassOf(typeof(Controller)) || t.IsSubclassOf(typeof(ControllerBase))).PropertiesAutowired();
        }
    }
} 
